<template>
  <section class="container">
    <div class="breadcrumb-container">
      <el-breadcrumb separator=">>">
        <el-breadcrumb-item :to="{ path: '/' }">商城首页</el-breadcrumb-item>
        <el-breadcrumb-item>商品列表</el-breadcrumb-item>
        <el-breadcrumb-item v-if="hasAccessories"
          ><span class="breadcrumb-a" @click="toGoodListByType(2)">{{
            accessoriesText
          }}</span></el-breadcrumb-item
        >
        <el-breadcrumb-item v-else
          ><span
            class="breadcrumb-a"
            @click="toGoodListByDeciveType(goodsDetail.deviceType)"
            >{{ deviceTypeText }}</span
          ></el-breadcrumb-item
        >
        <el-breadcrumb-item
          ><span
            class="breadcrumb-a"
            @click="
              toGoodList(
                goodsDetail.firstCategory,
                goodsDetail.firstCategoryName,
                goodsDetail.deviceType
              )
            "
            >{{ goodsDetail.firstCategoryName }}</span
          ></el-breadcrumb-item
        >
        <el-breadcrumb-item>{{ goodsDetail.name }}</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="menu">
      <el-menu
        default-active="introduction"
        mode="horizontal"
        @select="handleSelect"
      >
        <el-menu-item index="introduction">产品概况</el-menu-item>
        <el-menu-item index="pictures">实物图片</el-menu-item>
        <el-menu-item index="parameterize">参数配置</el-menu-item>
        <el-menu-item index="accessories" v-if="isCustomer"
          >相关配件</el-menu-item
        >
      </el-menu>
    </div>
    <div class="main">
      <div class="introduction" ref="introduction">
        <el-image
          style="width: 440px; height: 330px"
          :src="goodsDetail.mainPhoto"
          fit="fit"
        ></el-image>
        <div class="content">
          <h3 class="name">{{ goodsDetail.name }}</h3>
          <div class="descrition">{{ goodsDetail.recommend }}</div>
          <div class="specification">
            <template v-for="(item, index) in goodsDetail.specification">
              <div class="item" :key="item.id" v-if="index < 6">
                <div class="value">{{ item.value }}</div>
                <div class="key">{{ item.key }}</div>
              </div>
            </template>
          </div>
          <footer>
            <el-button
              type="primary"
              class="big-btn"
              @click="toInquiry(goodsDetail.id)"
              >立即询价</el-button
            >
          </footer>
        </div>
      </div>
      <div class="block descrition" ref="descrition">
        <div class="title-box">
          <span class="title">{{ goodsDetail.name }}产品介绍</span>
        </div>
        <div class="content" v-html="content"></div>
      </div>
      <div class="block pictures" ref="pictures">
        <div class="title-box">
          <span class="title">{{ goodsDetail.name }}图片</span>
        </div>
        <div class="content">
          <template v-for="(item, index) in goodsDetail.photoList">
            <div class="item" :key="index">
              <el-image
                style="width: 220px; height: 165px"
                :src="item"
                fit="fit"
              ></el-image>
            </div>
          </template>
        </div>
      </div>
      <div class="block parameterize" ref="parameterize">
        <div class="title-box">
          <span class="title">{{ goodsDetail.name }}参数配置</span>
        </div>
        <div class="content">
          <el-table
            :data="goodsDetail.specification"
            style="width: 100%"
            border
            header-cell-class-name="cell-s table-header-bg"
            cell-class-name="cell-s"
          >
            <el-table-column prop="key" label="参数名称" width="600">
            </el-table-column>
            <el-table-column
              prop="value"
              label="参数值"
              width="599"
            ></el-table-column>
          </el-table>
        </div>
      </div>
      <div class="block accessories" ref="accessories" v-if="isCustomer">
        <div class="title-box">
          <span class="title">{{ goodsDetail.name }}相关配件</span>
        </div>
        <div class="content">
          <swiper ref="mySwiper" :options="swiperOptions">
            <template v-for="item in accessories">
              <swiper-slide :key="item.id">
                <div class="item">
                  <el-image
                    style="width: 220px; height: 165px"
                    :src="item.image"
                    fit="fit"
                  ></el-image>
                  <div class="name">{{ item.name }}</div>
                </div>
              </swiper-slide>
            </template>
            <div
              v-if="accessories.length > 0"
              class="swiper-pagination"
              slot="pagination"
            ></div>
            <!-- <div class="swiper-button-prev"></div>
            <div class="swiper-button-next"></div> -->
          </swiper>
        </div>
      </div>
    </div>
  </section>
</template>

<script>
import { swiper, swiperSlide } from "vue-awesome-swiper";
import "swiper/dist/css/swiper.css";
import { getGoodsDetail, getModelAccessories } from "@/api/api.js";
export default {
  name: "GoodsDetail",
  components: {
    swiper,
    swiperSlide,
  },
  data() {
    return {
      goodsDetail: {},
      accessories: [],
      swiperOptions: {
        slidesPerView: 5, //显示个数
        pagination: { el: ".swiper-pagination", clickable: true },
        // navigation: {
        //   nextEl: ".swiper-button-next",
        //   prevEl: ".swiper-button-prev",
        // },
      },
      accessoriesText: "配件类",
      deviceType: {
        1: "搬运类",
        2: "输送类",
        3: "包装类",
        4: "其他类",
      },
    };
  },
  mounted() {
    this.getGoodsDetail(
      this.$router.currentRoute.query.id,
      this.$router.currentRoute.query.type
    );
  },
  computed: {
    content() {
      return "<p>" + this.goodsDetail.description + "</p>";
    },
    idCustomer() {
      let mallLoginInfo = null;
      try {
        mallLoginInfo = JSON.parse(localStorage.getItem("mallLoginInfo"));
      } catch (e) {
        // console.info(e)
      }
      return mallLoginInfo?.token;
    },
    hasAccessories() {
      return this.goodsDetail.type === 2 ? true : false;
    },
    deviceTypeText() {
      return this.deviceType[this.goodsDetail.deviceType];
    },
    isCustomer() {
      let mallLoginInfo = null,
        result = false;
      try {
        mallLoginInfo = JSON.parse(localStorage.getItem("mallLoginInfo"));
      } catch (e) {
        // console.info(e)
      }
      if (mallLoginInfo) {
        if (mallLoginInfo.token) {
          result = true;
        } else {
          result = false;
        }
      }
      return result;
    },
  },
  methods: {
    handleSelect(key, keyPath) {
      console.info(key, keyPath);
      let distance = this.$refs[key].offsetTop - 185 - 31;
      document.getElementById("app").scrollTop = distance;
    },
    getGoodsDetail(id, type) {
      getGoodsDetail({ page: 1, id, type }).then((res) => {
        console.info("设备商品详情", res);
        this.goodsDetail = res.data;
        this.goodsDetail.specification = [];
        this.goodsDetail.photoList = [];
        try {
          this.goodsDetail.specification = JSON.parse(res.data.specification);
        } catch (e) {
          // console.info(e)
        }
        try {
          this.goodsDetail.photoList = JSON.parse(res.data.photoList);
        } catch (e) {
          // console.info(e)
        }
        console.info(this.goodsDetail.photoList);
        if (this.idCustomer) {
          this.getModelAccessories(res.data.modelId, res.data.id);
        }
      });
    },
    getModelAccessories(id, goodsId) {
      getModelAccessories({ id, goodsId }).then((res) => {
        console.info("商品相关配件", res);
        this.accessories = res.data || [];
      });
    },
    toInquiry(id) {
      this.$store.dispatch("user/updateGoodId", { id: id });
    },
    toGoodList(firstCategory, firstCategoryName, topid) {
      this.$store.dispatch("user/getGoodsList", {
        firstCategory,
        firstCategoryName,
        deviceType: topid,
      });
      if (this.$router.currentRoute.name !== "GoodsList") {
        this.$router.push({ path: "/goodsList" });
      }
    },
    toGoodListByDeciveType(deviceType) {
      this.$store.dispatch("user/getGoodsList", { deviceType });
      if (this.$router.currentRoute.name !== "GoodsList") {
        this.$router.push({ path: "/goodsList" });
      }
    },
    toGoodListByType(type) {
      this.$store.dispatch("user/getGoodsList", { type });
      if (this.$router.currentRoute.name !== "GoodsList") {
        this.$router.push({ path: "/goodsList" });
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.container {
  border-top: 1px solid #ebeef5;
  color: #303133;

  .breadcrumb-container {
    width: 1200px;
    margin: 15px auto;
  }
  .menu {
    background-color: #3378ff;
    .el-menu {
      width: 1200px;
      margin: auto;
      background-color: transparent;
      border: 1px solid transparent;

      .el-menu-item {
        border: 1px solid transparent;
        color: #ffffff;
      }
      .el-menu-item:hover {
        color: #303133;
      }
      .el-menu-item.is-active {
        background-color: #ffffff;
        color: #303133;
      }
    }
  }

  .main {
    width: 1200px;
    margin: auto;

    .introduction {
      margin-top: 45px;
      display: flex;
      justify-content: space-between;

      .content {
        text-align: left;
        width: 730px;
        color: #303133;

        .name {
          font-weight: bold;
          font-size: 30px;
          line-height: 40px;
          padding-bottom: 20px;
          border-bottom: 1px solid #ebeef5;
          margin-bottom: 15px;
        }
        .descrition {
          font-size: 14px;
          line-height: 25px;
          margin-bottom: 15px;
        }
        .specification {
          height: 95px;
          background-color: #f8f7f7;
          margin-bottom: 15px;
          display: flex;
          align-items: center;

          .item {
            text-align: center;
            flex-grow: 1;

            .value {
              font-size: 20px;
              line-height: 26px;
              color: #000000;
              margin-bottom: 10px;
            }
            .key {
              font-size: 14px;
              line-height: 18px;
              color: #909399;
            }
          }
          .item:not(:first-child) {
            border-left: 1px solid #ebeef5;
          }
        }
        .big-btn {
          font-size: 20px;
          padding: 18px 83px;
          border-radius: 0;
        }
      }
    }
    .block {
      text-align: left;

      .title-box {
        border-bottom: 2px solid #ebeef5;
        color: #303133;
        margin-bottom: 30px;

        .title {
          display: inline-block;
          height: 100%;
          padding: 20px 0 13px;
          font-weight: bold;
          font-size: 20px;
          line-height: 26px;
          border-bottom: 2px solid #3378ff;
        }
      }
    }
    .pictures {
      .content {
        display: flex;
        justify-content: flex-start;
        flex-wrap: wrap;

        .item {
          width: 240px;
          padding: 10px;
          overflow: hidden;
        }
      }
    }
    .accessories {
      .item {
        padding: 0 10px;

        .name {
          font-size: 14px;
          line-height: 18px;
          color: #606266;
          margin-top: 10px;
          text-align: center;
        }
      }
    }
  }
  .main > * {
    margin-bottom: 60px;
  }
  .swiper-pagination {
    position: static;
    display: flex;
    justify-content: flex-start;
    margin-top: 10px;
    margin-bottom: 5px;

    ::v-deep .swiper-pagination-bullet {
      flex-grow: 1;
      margin: 0;
      border-radius: 0;
      height: 1px;
    }
    ::v-deep .swiper-pagination-bullet-active {
      opacity: 1;
      background: #3378ff;
      outline: 1px solid #3378ff;
    }
  }
}
</style>
<style lang="scss">
.table-header-bg {
  background-color: #f2f6fc !important;

  .cell {
    font-size: 16px;
    font-weight: normal;
    color: #303133 !important;
  }
}
.cell-s {
  padding: 18px 0 !important;
  background-color: #fafafa;

  .cell {
    color: #606266;
    padding-left: 50px !important;
  }
}
</style>
